package com.cy.ppj.service.message.impl;

import com.cy.ppj.comm.annotation.Message;
import com.cy.ppj.comm.utils.SpringELUtils;
import com.cy.ppj.model.domian.message.MessageRecord;
import com.cy.ppj.model.domian.message.MessageTemplate;
import com.cy.ppj.model.dto.message.OrderMessageDTO;
import com.cy.ppj.model.dto.wechat.WecahtTemplateDataDTO;
import com.cy.ppj.model.dto.wechat.WechatTemplateDTO;
import com.cy.ppj.model.enums.MessageType;
import com.cy.ppj.model.enums.OrderStatus;
import com.cy.ppj.model.enums.wechat.WechatTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: 订单消息
 * @author: Ocean
 * @create: 2018-08-09 19:15
 **/
@Slf4j
@Message(type = MessageType.ORDER)
@Service
public class OrderMessageImpl extends MessageServiceImpl {

    @Override
    public void execute(String message) {

        Long orderId = Long.valueOf(message);

        OrderMessageDTO template = messageTemplateMapper.selectOrderMessageTemplate(orderId);
        template.setOrderId(orderId);
        sendWechatTemplate(template);
        sendMessage(template);
    }

    /**
     * 发送站内信
     *
     * @param template
     */
    private void sendMessage(OrderMessageDTO template) {

        MessageTemplate messageTemplate = messageTemplateService.queryMessageTemplateByTypeInCache(MessageType.ORDER);
        if (messageTemplate == null) {
            log.info("订单[{}]状态更新,没有消息模版。", template.getOrderId());
            return;
        }
        if (!messageTemplate.getOpen()) {
            log.info("订单[{}]状态更新,消息模版未开启。", template.getOrderId());
            return;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("customer", template.getCustomerName());
        map.put("productName", template.getProductName());
        map.put("amount", template.getLoanAmount());
        map.put("status", template.getOrderStatus().getText());

        String content = SpringELUtils.getValue(messageTemplate.getContent(), map);

        MessageRecord messageRecord = new MessageRecord();
        messageRecord.setType(MessageType.ORDER);
        messageRecord.setContent(content);
        messageRecord.setUserId(template.getUserId());
        messageRecord.setCreateTime(new Date());
        messageRecord.setUrl("");

        messageRecordService.addMessageRecord(messageRecord);
    }

    /**
     * 发送微信模版
     *
     * @param template
     */
    private void sendWechatTemplate(OrderMessageDTO template) {

        if (template.getOpenId() == null) {
            log.info("订单[{}]状态更新,没有绑定微信,无法发送微信消息。", template.getOrderId());
            return;
        }

        WechatTemplateDTO send = new WechatTemplateDTO();
        send.setOpneId(template.getOpenId());
        send.setAccessToken(wechatService.getAccessToken());
        send.setTemplateId(WechatTemplate.ORDER.getTemplateId());

        WecahtTemplateDataDTO first = new WecahtTemplateDataDTO();
        first.setValue("您的客户订单状态已更新");

        WecahtTemplateDataDTO keyword1 = new WecahtTemplateDataDTO();
        keyword1.setValue(template.getCustomerName());

        WecahtTemplateDataDTO keyword2 = new WecahtTemplateDataDTO();
        keyword2.setValue(template.getProductName());

        BigDecimal amount = template.getLoanAmount().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
        WecahtTemplateDataDTO keyword3 = new WecahtTemplateDataDTO();
        keyword3.setValue(amount.toString().concat("万"));

        WecahtTemplateDataDTO keyword4 = new WecahtTemplateDataDTO();
        keyword4.setValue(template.getPeriods().toString().concat("个月"));

        OrderStatus orderStatus = template.getOrderStatus();
        WecahtTemplateDataDTO keyword5 = new WecahtTemplateDataDTO();
        keyword5.setValue(orderStatus.getText());

        WecahtTemplateDataDTO remark = new WecahtTemplateDataDTO();
        remark.setValue("详情可进入匹配家，在我-我的订单中查看");

        send.getData().put("first", first);
        send.getData().put("keyword1", keyword1);
        send.getData().put("keyword2", keyword2);
        send.getData().put("keyword3", keyword3);
        send.getData().put("keyword4", keyword4);
        send.getData().put("keyword5", keyword5);
        send.getData().put("remark", remark);

        wechatService.sendTemplateMessage(send);

    }
}
